/*
	Cross Platform Core Code.

	Copyright(R) 2001-2002 Balang Software.
	All rights reserved.

	Using:
		class	CBondReport;
*/

#include	"tfBondInfo.h"


bool InTradeTime(time_t tm, int nInflateSeconds)
{
	QDateTime t = QDateTime::fromTime_t(tm);

	// 周六周日
	if (7 == t.date().dayOfWeek()/*GetDayOfWeek()*/)
		return false;
	else if (1 == t.date().dayOfWeek()/*GetDayOfWeek()*/)
		return false;

	time_t	day = (t.toTime_t()/*GetTime()*/ + 8 * 3600) % 86400;

	if (day >= 9 * 3600 + 15 * 60 - nInflateSeconds && day <= 11 * 3600 + 30 * 60 + nInflateSeconds)
		return true;
	else if (day >= 13 * 3600 - nInflateSeconds && day <= 15 * 3600 + 15 * 60 + nInflateSeconds)
		return true;
	return false;
}

/////////////////////////////////////////////////////////////////////////////

CBondReport::CBondReport()
{
	m_pData = NULL;
	m_nSize = m_nMaxSize = m_nGrowBy = 0;
}

CBondReport::CBondReport( const CBondReport &src )
{
	m_pData = NULL;
	m_nSize = m_nMaxSize = m_nGrowBy = 0;

	*this	=	src;
}

CBondReport::~CBondReport()
{
 	if( m_pData )
 		free((BYTE*)m_pData);
		//delete [] (BYTE*)m_pData;
}

void CBondReport::SetSize(int nNewSize, int nGrowBy /* = -1 */)
{
	Q_ASSERT(nNewSize >= 0);

	if (nGrowBy != -1)
		m_nGrowBy = nGrowBy;  // set new size

	if (nNewSize == 0)
	{
		// shrink to nothing
		//delete [] (BYTE*)m_pData;
		free((BYTE*)m_pData);
		m_pData = NULL;
		m_nSize = m_nMaxSize = 0;
	}
	else if (m_pData == NULL)
	{
		// create one with exact size
#ifdef SIZE_T_MAX
		Q_ASSERT((long)nNewSize * sizeof(REPORTF) <= SIZE_T_MAX);  // no overflow
#endif
		//m_pData = (REPORTF*) new BYTE[nNewSize * sizeof(REPORTF)];
		m_pData = (REPORTF *)malloc(nNewSize * sizeof(REPORTF));

		memset(m_pData, 0, nNewSize * sizeof(REPORTF));  // zero fill

		m_nSize = m_nMaxSize = nNewSize;
	}
	else if (nNewSize <= m_nMaxSize)
	{
		// it fits
		if (nNewSize > m_nSize)
		{
			// initialize the new elements

			memset(&m_pData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(REPORTF));

		}

		m_nSize = nNewSize;
	}
	else
	{
		// Otherwise grow array
		int nNewMax;
		if (nNewSize < m_nMaxSize + m_nGrowBy)
			nNewMax = m_nMaxSize + m_nGrowBy;  // granularity
		else
			nNewMax = nNewSize;  // no slush

#ifdef SIZE_T_MAX
		Q_ASSERT((long)nNewMax * sizeof(REPORTF) <= SIZE_T_MAX);  // no overflow
#endif
		//REPORTF* pNewData = (REPORTF*) new BYTE[nNewMax * sizeof(REPORTF)];
		REPORTF* pNewData = (REPORTF *)malloc(nNewMax * sizeof(REPORTF));

		// copy new data from old
		memcpy(pNewData, m_pData, m_nSize * sizeof(REPORTF));

		// construct remaining elements
		Q_ASSERT(nNewSize > m_nSize);

		memset(&pNewData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(REPORTF));


		// get rid of old stuff (note: no destructors called)
		//delete [] (BYTE*)m_pData;
		free((BYTE*)m_pData);
		m_pData = pNewData;
		m_nSize = nNewSize;
		m_nMaxSize = nNewMax;
	}
}

void CBondReport::FreeExtra()
{
	if (m_nSize != m_nMaxSize)
	{
		// shrink to desired size
#ifdef SIZE_T_MAX
		Q_ASSERT((long)m_nSize * sizeof(REPORTF) <= SIZE_T_MAX);  // no overflow
#endif
		REPORTF* pNewData = NULL;
		if (m_nSize != 0)
		{
			//pNewData = (REPORTF*) new BYTE[m_nSize * sizeof(REPORTF)];
			pNewData = (REPORTF *)malloc(m_nSize * sizeof(REPORTF));
			// copy new data from old
			memcpy(pNewData, m_pData, m_nSize * sizeof(REPORTF));
		}

		// get rid of old stuff (note: no destructors called)
		//delete [] (BYTE*)m_pData;
		free((BYTE*)m_pData);
		m_pData = pNewData;
		m_nMaxSize = m_nSize;
	}
}

/////////////////////////////////////////////////////////////////////////////

void CBondReport::SetAtGrow(int nIndex, const REPORTF & newElement)
{
	Q_ASSERT(nIndex >= 0);

	if (nIndex >= m_nSize)
		SetSize(nIndex+1);
	m_pData[nIndex] = newElement;
}

void CBondReport::InsertAt(int nIndex, const REPORTF & newElement, int nCount /*=1*/)
{
	Q_ASSERT(nIndex >= 0);    // will expand to meet need
	Q_ASSERT(nCount > 0);     // zero or negative size not allowed

	if (nIndex >= m_nSize)
	{
		// adding after the end of the array
		SetSize(nIndex + nCount);  // grow so nIndex is valid
	}
	else
	{
		// inserting in the middle of the array
		int nOldSize = m_nSize;
		SetSize(m_nSize + nCount);  // grow it to new size
		// shift old data up to fill gap
		memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
			(nOldSize-nIndex) * sizeof(REPORTF));

		// re-init slots we copied from

		memset(&m_pData[nIndex], 0, nCount * sizeof(REPORTF));

	}

	// insert new value in the gap
	Q_ASSERT(nIndex + nCount <= m_nSize);
	while (nCount--)
		m_pData[nIndex++] = newElement;
}

void CBondReport::RemoveAt(int nIndex, int nCount /* = 1 */)
{
	Q_ASSERT(nIndex >= 0);
	Q_ASSERT(nCount >= 0);
	Q_ASSERT(nIndex + nCount <= m_nSize);

	// just remove a range
	int nMoveCount = m_nSize - (nIndex + nCount);

	if (nMoveCount)
		memcpy(&m_pData[nIndex], &m_pData[nIndex + nCount],
			nMoveCount * sizeof(REPORTF));
	m_nSize -= nCount;
}

void CBondReport::InsertAt(int nStartIndex, CBondReport* pNewArray)
{
	Q_ASSERT(pNewArray != NULL);
	Q_ASSERT(nStartIndex >= 0);

	if (pNewArray->GetSize() > 0)
	{
		InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
		for (int i = 0; i < pNewArray->GetSize(); i++)
			SetAt(nStartIndex + i, pNewArray->GetAt(i));
	}
}

int CBondReport::InsertReportSort( const REPORTF & newElement )
{
	if( newElement.m_time <= 0 )
		return -1;
	if( newElement.m_fNew < 1e-4 )
		return -1;
	if( newElement.m_fVolume < 1e-4 )
		return -1;
	//if( !CSPTime::InTradeTime( newElement.m_time, 60 ) )
	//	return -1;

	for( int i=GetSize()-1; i>=0; i-- )
	{
		REPORTF & temp = ElementAt(i);
		if( newElement.m_time == temp.m_time && 
			newElement.m_nUpdateMillisec==temp.m_nUpdateMillisec)//以后要添加毫秒判断
		{
			SetAt(i,newElement);
			return i;
		}
		if( fabs(newElement.m_fVolume - temp.m_fVolume) < 1e-4 )
		{
			SetAt(i,newElement);
			return i;
		}
		if( newElement.m_time >= temp.m_time && newElement.m_fVolume > temp.m_fVolume )
		{
			InsertAt(i+1,newElement);
			return i+1;
		}
		if( newElement.m_time > temp.m_time && newElement.m_fVolume < temp.m_fVolume )
			return -1;
		if( newElement.m_time < temp.m_time && newElement.m_fVolume > temp.m_fVolume )
			return -1;
	}
	
	InsertAt( 0, newElement );
	return 0;
}

int SortReport(const void *p1,const void *p2)
{
	REPORTF *pTemp1 = (REPORTF *)p1;
	REPORTF *pTemp2 = (REPORTF *)p2;

	if( pTemp1 && pTemp2 && pTemp1->m_time < pTemp2->m_time )
		return -1;
	else if( pTemp1 && pTemp2 && pTemp1->m_time > pTemp2->m_time )
		return 1;
//	if( pTemp1 && pTemp2 && pTemp1->m_fVolume < pTemp2->m_fVolume )
//		return -1;
//	else if( pTemp1 && pTemp2 && pTemp1->m_fVolume > pTemp2->m_fVolume )
//		return 1;
	return 0;
}

void CBondReport::Sort( )
{
	if( m_pData )
		qsort( m_pData, GetSize(), sizeof(REPORTF), SortReport );
}


void CBondReport::RemoveDirty( )
{
	for( int i=GetSize()-1; i>=0; i-- )
	{
		if( ElementAt(i).m_time <= 0 )
			RemoveAt(i);
//		else if( ElementAt(i).m_fNew < 1e-4 )
//			RemoveAt(i);
//		else if( ElementAt(i).m_fVolume < 1e-4 )
//			RemoveAt(i);
		else if( !InTradeTime( ElementAt(i).m_time, 60 ) )
			RemoveAt(i);
//		else if( i>0 && ElementAt(i).m_fVolume - ElementAt(i-1).m_fVolume < 1e-4 )
//			RemoveAt(i);
	}
}

CBondReport	& CBondReport::operator = ( const CBondReport &src )
{
	Copy( src );
	return	*this;
}

void CBondReport::Copy( const CBondReport &src )
{
	SetSize( 0, src.GetSize()+5 );
	for( int i=0; i<src.GetSize(); i++ )
	{
		Add( src.GetAt(i) );
	}
}

bool CBondReport::GetMMLD( int nIndex, double *pdVolBuy, double *pdVolSell, double * pdVolDiff )
{
	Q_ASSERT( nIndex >= 0 && nIndex < GetSize() );
	if( nIndex < 0 || nIndex > GetSize()-1 )
		return false;
    //int k;
	double	dVolBuy = 0;
	double	dVolSell = 0;
	REPORTF & rpt = ElementAt(nIndex);
	//for( k=0; k<sizeof(rpt.m_fBuyVolume)/sizeof(rpt.m_fBuyVolume[0]); k++ )
	//	dVolBuy		+=	rpt.m_fBuyVolume[k];
	//for( k=0; k<sizeof(rpt.m_fSellVolume)/sizeof(rpt.m_fSellVolume[0]); k++ )
	//	dVolSell	+=	rpt.m_fSellVolume[k];
	
	if( pdVolBuy )	*pdVolBuy	=	dVolBuy;
	if( pdVolSell )	*pdVolSell	=	dVolSell;
	if( pdVolDiff )	*pdVolDiff	=	(dVolBuy-dVolSell);
	return true;
}

bool CBondReport::GetMMLDMinMaxInfo( double *pdMin, double *pdMax )
{
	double	dMin = 0, dMax = 1;
	for( int i=0; i<GetSize(); i++ )
	{
		double	dVolBuy = 0;
		double	dVolSell = 0;
		GetMMLD( i, &dVolBuy, &dVolSell, NULL );

		if( 0 == i )
		{
			dMin	=	dVolBuy;
			dMax	=	dVolBuy;
		}
		if( dVolBuy < dMin )	dMin	=	dVolBuy;
		if( dVolBuy > dMax )	dMax	=	dVolBuy;
		if( dVolSell < dMin )	dMin	=	dVolSell;
		if( dVolSell > dMax )	dMax	=	dVolSell;
	}

	if( dMax < 500 )
		dMax	=	500;	// 最小为5手

	if( pdMin )	*pdMin	=	dMin;
	if( pdMax )	*pdMax	=	dMax;
	return true;
}

bool CBondReport::StatBuySellEx( float * fSellPrice, float * fSellVolume, float * fBuyPrice, float * fBuyVolume, int nSize )
{
/*	int k;
	for(  k=0; k<nSize; k++ )
	{
		fSellPrice[k]	=	0.;
		fSellVolume[k]	=	0.;
		fBuyPrice[k]	=	0.;
		fBuyVolume[k]	=	0.;
	}

	CSPDWordArray	adwSellPrice, adwSellVolume, adwBuyPrice, adwBuyVolume;
	REPORTF	rpt;
	memset( &rpt, 0, sizeof(rpt) );
	int	i,nBSCount	=	sizeof(rpt.m_fBuyPrice)/sizeof(float);

	for( k=0; k<GetSize(); k++ )
	{
		REPORTF	& report	=	ElementAt(k);
		
		// Insert
		for(  i=nBSCount-1; i>=0; i-- )
		{
			if( report.m_fSellPrice[i] > 1e-4 )
			{
				adwSellPrice.InsertAt( 0, DWORD(report.m_fSellPrice[i]*1000) );
				adwSellVolume.InsertAt( 0, DWORD(report.m_fSellVolume[i]) );
			}
			if( report.m_fBuyPrice[i] > 1e-4 )
			{
				adwBuyPrice.InsertAt( 0, DWORD(report.m_fBuyPrice[i]*1000) );
				adwBuyVolume.InsertAt( 0, DWORD(report.m_fBuyVolume[i]) );
			}
		}

		// Remove 
		DWORD	dwSellMin = 0;
		for( i=0; i<adwSellPrice.GetSize(); i++ )
		{
			if( adwSellPrice[i] < dwSellMin+1e-4 )
			{
				adwSellPrice.RemoveAt(i);
				adwSellVolume.RemoveAt(i);
				i --;
			}
			else
			{
				dwSellMin	=	adwSellPrice[i];
			}
		}

		DWORD	dwBuyMax = 0;
		for( i=0; i<adwBuyPrice.GetSize(); i++ )
		{
			if( 0 != dwBuyMax && adwBuyPrice[i] > dwBuyMax-1e-4 )
			{
				adwBuyPrice.RemoveAt(i);
				adwBuyVolume.RemoveAt(i);
				i --;
			}
			else
			{
				dwBuyMax	=	adwBuyPrice[i];
			}
		}
	}

	// Store
	for( k=0; k<nSize && k<adwSellPrice.GetSize(); k++ )
	{
		fSellPrice[k]	=	float(0.001 * adwSellPrice[k]);
		fSellVolume[k]	=	float(adwSellVolume[k]);
	}
	for( k=0; k<nSize && k<adwBuyPrice.GetSize(); k++ )
	{
		fBuyPrice[k]	=	float(0.001 * adwBuyPrice[k]);
		fBuyVolume[k]	=	float(adwBuyVolume[k]);
	}
*/
	return true;
}

bool CBondReport::StatVolumeInfo( double *pdVolNow, double *pdVolOuter, double *pdVolInner )
{
	time_t	tmLatest	=	0;
	double	dVolNow = 0, dVolOuter = 0, dVolInner = 0;
	double	dPriceLast	=	-1;
	bool	bRise		=	true;
/*	for( int k=0; k<GetSize(); k++ )
	{
		if( ElementAt(k).m_time > tmLatest )
		{
			tmLatest	=	ElementAt(k).m_time;
			dVolNow		=	ElementAt(k).m_fVolume;
		}
		double	dPrice	=	ElementAt(k).m_fNew;
		if( 0 != k )
		{
			double	dVolume	=	ElementAt(k).m_fVolume - ElementAt(k-1).m_fVolume;
			if( dPrice - dPriceLast > 1e-4 )
				bRise	=	true;
			if( dPrice - dPriceLast < -1e-4 )
				bRise	=	false;
			if( bRise )	dVolOuter	+=	dVolume;
			else		dVolInner	+=	dVolume;
		}
		dPriceLast	=	dPrice;
	}
*/
	if( pdVolNow )
		*pdVolNow = dVolNow;
	if( pdVolOuter )
		*pdVolOuter = dVolOuter;
	if( pdVolInner )
		*pdVolInner = dVolInner;
	return true;
}

/////////////////////////////////////////////////////////////////////////////



